<script setup>
import { onMounted, ref } from 'vue'
// import { useUserStore } from '@/store'
import { getArticleChannels, delArticleChannels } from '@/api/article.js'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelEdit from './components/ChannelEdit.vue'
const ChannelList = ref([])
const getChannelList = async () => {
  loading.value = true
  const res = await getArticleChannels()
  ChannelList.value = res.data.data
  // console.log(ChannelList.value)
  loading.value = false
}
const loading = ref(false)
const dialog = ref()
onMounted(() => {
  getChannelList()
})

const onEditChannel = async (row) => {
  // console.log(row)
  dialog.value.open(row)
}
const onDelChannel = (row) => {
  ElMessageBox.confirm('你确定要删除该分类吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await delArticleChannels(row.id)
    getChannelList()
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
  })
}
const onSuccess = () => {
  getChannelList()
}
</script>
<template>
  <page-container title="文件分类">
    <template #extra>
      <el-button type="primary" @click="onEditChannel({})">
        添加分类
      </el-button>
    </template>
    <el-table :data="ChannelList" style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            :icon="Edit"
            @click="onEditChannel(row, $index)"
            plain
            circle
          ></el-button>
          <el-button
            type="danger"
            circle
            :icon="Delete"
            plain
            @click="onDelChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
  </page-container>
</template>

<style lang="scss" scoped></style>
